#include <engine.h>
#include <wiiuse/wpad.h>

//emet-engine example 01
//basic engine initialization and drawing

void DrawCube();
float rotation;
float vertices[]  = {   -1.0f,  1.0f, -1.0f,
                        -1.0f,  1.0f,  1.0f,
                        -1.0f, -1.0f,  1.0f,
                        -1.0f, -1.0f, -1.0f,
                         1.0f,  1.0f, -1.0f,
                         1.0f, -1.0f, -1.0f,
                         1.0f, -1.0f,  1.0f,
                         1.0f,  1.0f,  1.0f };

int main(int argc, char *argv[]) {
	WPAD_Init();
	
	//Allocatate and init a new Engine object
	Engine *engine = new Engine();
	engine->Init();
	
	//Create a scene
	Scene *scene = new Scene();
	engine->SetScene(scene);
	
	//Create a camera
	Camera *cam = new Camera();
	
	//Attach camera to the scene's root node
	scene->GetRootNode()->addObject(cam);
	
	//Activate the camera and move it
	scene->SetActiveCamera(cam);
	cam->Set(Vector3D(4.0f, 4.0f, 4.0f), Vector3D(0.0f, 0.0f, 0.0f), Vector3D(0.0f, 0.0f, 1.0f) );
	cam->SetFOV(33.3f);
	
	//Prepare cube animation
	rotation = 0.0f;
	
	//Set current render mode
	engine->SetRenderMode(ENGINE_MODE_3D);
	
	//Create a node to
	while(1) {
		WPAD_ScanPads();
		
		//Begin drawing 
		engine->StartDrawing();
		
		//Draw a simple cube
		DrawCube();
		rotation += 2.0f;
		
		engine->EndDrawing();
		
		//Render the buffers on the screen
		engine->Render();
		
		if(WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) break;
	}
	
	return 0;
}

void DrawCube()
{
	Mtx r, t, m;
	guVector  axis = (guVector){1, 0, 1};
	
	guMtxIdentity(t);
    guMtxTransApply(t, t, 0, 0, 0);
	guMtxRotAxisDeg(r, &axis, rotation);

    guMtxConcat(t, r, t);
	guMtxConcat(Engine::GX_ModelViewMtx, t, m);
	GX_LoadPosMtxImm(m, GX_PNMTX0);
	GX_SetCurrentMtx(GX_PNMTX0);

    GX_Begin(GX_QUADS, GX_VTXFMT0, 4*6);	
		GX_Position3f32(vertices[0], vertices[1], vertices[2]);
		GX_Color4u8(255, 255, 255, 255);
		GX_Position3f32(vertices[3], vertices[4], vertices[5]);
		GX_Color4u8(255, 255, 255, 255);
		GX_Position3f32(vertices[6], vertices[7], vertices[8]);
		GX_Color4u8(255, 255, 255, 255);
		GX_Position3f32(vertices[9], vertices[10], vertices[11]);
		GX_Color4u8(255, 255, 255, 255);
		
		GX_Position3f32(vertices[12], vertices[13], vertices[14]);
		GX_Color4u8(0, 255, 255, 255);
		GX_Position3f32(vertices[15], vertices[16], vertices[17]);
		GX_Color4u8(0, 255, 255, 255);
		GX_Position3f32(vertices[18], vertices[19], vertices[20]);
		GX_Color4u8(0, 255, 255, 255);
		GX_Position3f32(vertices[21], vertices[22], vertices[23]);
		GX_Color4u8(0, 255, 255, 255);		
	
		GX_Position3f32(vertices[6], vertices[7], vertices[8]);
		GX_Color4u8(255, 0, 255, 255);
		GX_Position3f32(vertices[18], vertices[19], vertices[20]);
		GX_Color4u8(255, 0, 255, 255);
		GX_Position3f32(vertices[15], vertices[16], vertices[17]);
		GX_Color4u8(255, 0, 255, 255);
		GX_Position3f32(vertices[9], vertices[10], vertices[11]); 
		GX_Color4u8(255, 0, 255, 255);		
        
		GX_Position3f32(vertices[3], vertices[4], vertices[5]);
		GX_Color4u8(255, 255, 0, 255);
		GX_Position3f32(vertices[0], vertices[1], vertices[2]);
		GX_Color4u8(255, 255, 0, 255);
		GX_Position3f32(vertices[12], vertices[13], vertices[14]);
		GX_Color4u8(255, 255, 0, 255);
		GX_Position3f32(vertices[21], vertices[22], vertices[23]);
		GX_Color4u8(255, 255, 0, 255);		
		
		GX_Position3f32(vertices[15], vertices[16], vertices[17]);
		GX_Color4u8(0, 255, 0, 255);
		GX_Position3f32(vertices[12], vertices[13], vertices[14]);
		GX_Color4u8(0, 255, 0, 255);
		GX_Position3f32(vertices[0], vertices[1], vertices[2]);
		GX_Color4u8(0, 255, 0, 255);
		GX_Position3f32(vertices[6], vertices[7], vertices[8]);
		GX_Color4u8(0, 255, 0, 255);		
        
		GX_Position3f32(vertices[18], vertices[19], vertices[20]);
		GX_Color4u8(0, 0, 255, 255);
		GX_Position3f32(vertices[6], vertices[7], vertices[8]);
		GX_Color4u8(0, 0, 255, 255);
		GX_Position3f32(vertices[3], vertices[4], vertices[5]);
		GX_Color4u8(0, 0, 255, 255);
		GX_Position3f32(vertices[21], vertices[22], vertices[23]);
		GX_Color4u8(0, 0, 255, 255);		
    GX_End();
}
